<?php


namespace catchAdmin\apipatient\controller;


use app\common\lib\ZhyxMongo;
use app\common\model\BaseModel;
use app\common\model\PatientInfo;
use catcher\base\CatchController;
use catcher\CatchResponse;
use catcher\exceptions\CatchException;
use catcher\exceptions\FailedException;
use think\exception\ValidateException;
use think\Request;

class HealthDiary extends CatchController
{
    protected $business = null;

    public function __construct()
    {
        $this->business = new \catchAdmin\apipatient\service\HealthDiary();
    }

    public function index(Request $request): \think\response\Json
    {

        $postData = $request->param();
        $patientId = $postData["patientId"] ?? $request->userInfo["patientId"];

        $patientInfo = (new PatientInfo())->getPatientInfo($patientId, 3);

        //执行数据的查询操作
        //$guid = md5($postData["start_date"] . $postData["end_date"] . $patientId);
        $guid = hash("sha256", $postData["start_date"] . $postData["end_date"] . $patientId);
        $result = $this->business->getAHealthDiary($postData, $patientInfo->toArray());
        return CatchResponse::success(["guid" => $guid, "result" => $result]);
    }

    public function save(Request $request): \think\response\Json
    {
        $postData = $request->param();

        try {
            $patientId = $postData["patientId"] ?? $request->userInfo["patientId"];
            $patientInfo = (new PatientInfo())->getPatientInfo($patientId, 3);
            if (empty($patientInfo)) {
                throw new FailedException("患者不存在");
            }
            validate(\catchAdmin\apipatient\validate\HealthDiary::class)->scene("create_data")->check($postData);
            $result = $this->business->createHealthDiary($postData, $patientInfo->toArray());
            if ($result) {
                return CatchResponse::success();
            }
            return CatchResponse::fail("创建失败");
        } catch (ValidateException $validateException) {
            throw new FailedException($validateException->getMessage());
        } catch (\Exception $exception) {
            throw new FailedException($exception->getMessage());
        }
    }

    public function getLastHealthRecord(Request $request): \think\response\Json
    {
        $postData = $request->param();
        $patientId = $postData["patientId"] ?? $request->userInfo["patientId"];

        $result = $this->business->getLastAHealthDiary($patientId);
        return CatchResponse::success($result ?: []);
    }

    public function getHealRecordDetail(Request $request): \think\response\Json
    {
        $postData = $request->param();
        $startDate = $request->param("start_date") ?? date("Y-m-d");
        $patientId = $postData["patientId"] ?? $request->userInfo["patientId"];

        $type = $request->param("type", "bloodSugar", "string");
        $result = $this->business->getHealRecordDetail($startDate, $patientId, $type);
        return CatchResponse::success($result);
    }

    public function getHealRecordList(Request $request)
    {
        $startDate = $request->param("start_date") ?? date("Y-m-d");
        $endDate = $request->param("end_date") ?? date("Y-m-d");
        $patientId = $request->param("patientId") ?? $request->userInfo["patientId"];
        $uuid = hash("sha256", $startDate . $endDate . $patientId);

        $result = $this->business->getHealRecordList($startDate, $endDate, $patientId);

        return CatchResponse::success(["guid" => $uuid, "result" => $result]);
    }

}